"""
Case Type   :全文检索--解析器
Case Name   : 文本搜索配置token_type为zh_words（解析器为pg_catalog.ngram），不支持的编码，合理报错
Create At   : 2025/9/8
Owner       : @hejiahuan11
Description :
        1.创建文本搜索配置，解析器为pg_catalog.ngram
        2.创建simple字典
        3.增加文本搜索配置字串类型映射，token为zh_words
        4.使用文本检索函数对所创建的词典配置english2进行测试
        5.删除文本搜索配置；删除词典
Expect      :
        1.创建成功
        2.创建成功
        3.增加文本搜索配置字串类型映射成功
        4.合理报错
        5.删除成功
History     :
"""
import sys
import unittest
from yat.test import macro
from yat.test import Node
sys.path.append(sys.path[0]+"/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH


logger = Logger()
commonsh = CommonSH('dbuser')
constant = Constant()

class Parser(unittest.TestCase):
    def setUp(self):
        logger.info('------------------------Opengauss_Function_Text_Search_Configuration_Case0066开始执行-----------------------------')
        self.userNode = Node('dbuser')
        self.Constant = Constant()

    def test_ngram_parser(self):

        logger.info('------------创建文本搜索配置和词典-------------------')
        sql_cmd1 = commonsh.execut_db_sql('''drop text search configuration if exists english2 cascade;
                                            create text search configuration english2 (parser=ngram);
                                            drop text search dictionary if exists pg_dict cascade;
                                            create text search dictionary pg_dict (template = simple);''')
        logger.info(sql_cmd1)
        self.assertIn(self.Constant.CREATE_TEXT_SEARCH_CONFIGURATION, sql_cmd1)
        self.assertIn(self.Constant. CREATE_DICTIONARY_SUCCESS_MSG, sql_cmd1)
        logger.info('--------------------增加文本搜索配置字串类型映射，token为zh_words---------')
        sql_cmd2 = commonsh.execut_db_sql('''ALTER TEXT SEARCH CONFIGURATION english2 ADD MAPPING FOR zh_words WITH pg_dict;''')
        logger.info(sql_cmd2)
        self.assertIn(self.Constant.ALTER_TEXT_SEARCH_CONFIGURATION, sql_cmd2)
        logger.info('-----------使用文本检索函数对所创建的词典配置english2进行测试---------')
        sql_cmd3 = commonsh.execut_db_sql('''SELECT ts_debug('ngram','中文检索');''')
        logger.info(sql_cmd3)
        self.assertIn('ERROR:  ngram parser only support UTF8/GBK encoding', sql_cmd3)

    # 清理环境
    def tearDown(self):
        logger.info('----------this is teardown-------')
        sql_cmd4 = commonsh.execut_db_sql('''drop text search configuration english2 cascade;
                                           drop text search dictionary pg_dict cascade;''')
        logger.info(sql_cmd4)
        logger.info('-----------------------Opengauss_Function_Text_Search_Configuration_Case0066执行结束--------------------------')





